package com.weavernorth.humanresource.formal.service;


import com.engine.kq.biz.KQBalanceOfLeaveBiz;
import com.weavernorth.humanresource.formal.dao.FormalUserDao;
import com.weavernorth.humanresource.formal.dao.FormalUserDaoImpl;
import com.weavernorth.humanresource.formal.vo.FormalUser;
import com.weavernorth.util.Logger4jUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.apache.log4j.Logger;
import weaver.general.BaseBean;
import weaver.common.DateUtil;
import weaver.general.Util;
import weaver.workflow.webservices.*;


import java.util.List;

/**
 * @program: ecology_erdos
 * @description: 员工转正流程触发接口实现
 * @author: zm
 * @create: 2019-12-16 16:54
 **/
public class FormalUserServiceImpl implements FormalUserService {
//    private static Log log = LogFactory.getLog(FormalUserServiceImpl.class);
private Logger log = Logger4jUtil.getLoggerByName("/Timmer/humanresource/formal/formaluser/");
    /**
     * 查询所有试用员工信息
     */
    @Override
    public void createFormal() {
        FormalUserDao formalUserDao = new FormalUserDaoImpl();
        //得到所有员工
        List<FormalUser> listFormalUser = formalUserDao.getOnTryUsers();
        log.info("获取所有使用员工信息："+listFormalUser.toString());
        //当前日期
        String strCurrDate = DateUtil.getCurrentDate();

        //基础类
        BaseBean bb = new BaseBean();
        //得到流程的创建人
        String strCreater=Util.null2String(bb.getPropValue("humanMess","formalcreater"));
        log.info("得到流程创建人："+strCreater);
        //根据返回结果创建流程
        try {
            if (listFormalUser != null && listFormalUser.size() > 0) {
                for (FormalUser formalUser : listFormalUser) {
                    //部门负责人,如果部门负责人为空,则不创建流程
                    String strDepartCharge = Util.null2String(formalUser.getStrDepartmentCharge());
                    log.info("部门负责人：" + strDepartCharge);
                    if (!"".equals(strDepartCharge)) {
                        log.info("部门负责人不为空则创建流程：");
                        //开始创建流程
                        WorkflowRequestTableField[] wrti = new WorkflowRequestTableField[38];
                        //员工编码
                        String strWorkcode = Util.null2String(formalUser.getStrWorkcode());
                        wrti[0] = new WorkflowRequestTableField();
                        wrti[0].setFieldName("ygbh");
                        wrti[0].setFieldValue(Util.null2String(strWorkcode));
                        wrti[0].setView(true);
                        wrti[0].setEdit(true);
                        wrti[0].setFieldOrder(0);
                        wrti[0].setMand(false);

                        //证件号
                        String strCreNo = Util.null2String(formalUser.getStrCreNo());
                        wrti[1] = new WorkflowRequestTableField();
                        wrti[1].setFieldName("zjh");
                        wrti[1].setFieldValue(strCreNo);
                        wrti[1].setView(true);
                        wrti[1].setEdit(true);
                        wrti[1].setFieldOrder(0);
                        wrti[1].setMand(false);

                        //性别
                        String strSex = Util.null2String(formalUser.getStrSex());
                        wrti[2] = new WorkflowRequestTableField();
                        wrti[2].setFieldName("xb");
                        wrti[2].setFieldValue(strSex);
                        wrti[2].setView(true);
                        wrti[2].setEdit(true);
                        wrti[2].setFieldOrder(0);
                        wrti[2].setMand(false);
                        //出生日期
                        String strBirthDay = Util.null2String(formalUser.getStrBirthDay());
                        wrti[3] = new WorkflowRequestTableField();
                        wrti[3].setFieldName("csrq");
                        wrti[3].setFieldValue(strBirthDay);
                        wrti[3].setView(true);
                        wrti[3].setEdit(true);
                        wrti[3].setFieldOrder(0);
                        wrti[3].setMand(false);
                        //民族
                        String strNation = Util.null2String(formalUser.getStrNation());
                        wrti[4] = new WorkflowRequestTableField();
                        wrti[4].setFieldName("mz");
                        wrti[4].setFieldValue(strNation);
                        wrti[4].setView(true);
                        wrti[4].setEdit(true);
                        wrti[4].setFieldOrder(0);
                        wrti[4].setMand(false);
                        //国籍
                        String strNationality = Util.null2String(formalUser.getStrNationality());
                        wrti[5] = new WorkflowRequestTableField();
                        wrti[5].setFieldName("gj");
                        wrti[5].setFieldValue(strNationality);
                        wrti[5].setView(true);
                        wrti[5].setEdit(true);
                        wrti[5].setFieldOrder(0);
                        wrti[5].setMand(false);
                        //户口所在地
                        String strHukouAddress = Util.null2String(formalUser.getStrHukouAddress());
                        wrti[6] = new WorkflowRequestTableField();
                        wrti[6].setFieldName("hkszd");
                        wrti[6].setFieldValue(strHukouAddress);
                        wrti[6].setView(true);
                        wrti[6].setEdit(true);
                        wrti[6].setFieldOrder(0);
                        wrti[6].setMand(false);
                        //户口类型
                        String strHukouType = Util.null2String(formalUser.getStrHukouType());
                        wrti[7] = new WorkflowRequestTableField();
                        wrti[7].setFieldName("hklx");
                        wrti[7].setFieldValue(strHukouType);
                        wrti[7].setView(true);
                        wrti[7].setEdit(true);
                        wrti[7].setFieldOrder(0);
                        wrti[7].setMand(false);

                        //政治面貌
                        String strPolitic = Util.null2String(formalUser.getStrPolitic());
                        wrti[8] = new WorkflowRequestTableField();
                        wrti[8].setFieldName("zzmm");
                        wrti[8].setFieldValue(strPolitic);
                        wrti[8].setView(true);
                        wrti[8].setEdit(true);
                        wrti[8].setFieldOrder(0);
                        wrti[8].setMand(false);

                        //最高学历
                        String strDegree = Util.null2String(formalUser.getStrDegree());
                        wrti[9] = new WorkflowRequestTableField();
                        wrti[9].setFieldName("zgxl");
                        wrti[9].setFieldValue(strDegree);
                        wrti[9].setView(true);
                        wrti[9].setEdit(true);
                        wrti[9].setFieldOrder(0);
                        wrti[9].setMand(false);

                        //试用期
                        String strTyial = Util.null2String(formalUser.getStrTyial());
                        wrti[10] = new WorkflowRequestTableField();
                        wrti[10].setFieldName("syq");
                        wrti[10].setFieldValue(strTyial);
                        wrti[10].setView(true);
                        wrti[10].setEdit(true);
                        wrti[10].setFieldOrder(0);
                        wrti[10].setMand(false);
                        //入职日期
                        String strEntryDate = Util.null2String(formalUser.getStrEntryDate());
                        wrti[11] = new WorkflowRequestTableField();
                        wrti[11].setFieldName("rzsj");
                        wrti[11].setFieldValue(strEntryDate);
                        wrti[11].setView(true);
                        wrti[11].setEdit(true);
                        wrti[11].setFieldOrder(0);
                        wrti[11].setMand(false);

                        //转正日期
                        String strCurrectDate = Util.null2String(formalUser.getStrCurrectDate());
                        wrti[12] = new WorkflowRequestTableField();
                        wrti[12].setFieldName("zzrq");
                        wrti[12].setFieldValue(strCurrectDate);
                        wrti[12].setView(true);
                        wrti[12].setEdit(true);
                        wrti[12].setFieldOrder(0);
                        wrti[12].setMand(false);


                        //部门名称
                        String strDepartment = Util.null2String(formalUser.getStrDepartment());
                        wrti[13] = new WorkflowRequestTableField();
                        wrti[13].setFieldName("bm");
                        wrti[13].setFieldValue(strDepartment);
                        wrti[13].setView(true);
                        wrti[13].setEdit(true);
                        wrti[13].setFieldOrder(0);
                        wrti[13].setMand(false);
                        //岗位名称
                        String strJob = Util.null2String(formalUser.getStrJob());
                        wrti[14] = new WorkflowRequestTableField();
                        wrti[14].setFieldName("gw");
                        wrti[14].setFieldValue(strJob);
                        wrti[14].setView(true);
                        wrti[14].setEdit(true);
                        wrti[14].setFieldOrder(0);
                        wrti[14].setMand(false);
                        //员工组
                        String strStaff = Util.null2String(formalUser.getStrStaff());

                        wrti[15] = new WorkflowRequestTableField();
                        wrti[15].setFieldName("ygz");
                        wrti[15].setFieldValue(strStaff);
                        wrti[15].setView(true);
                        wrti[15].setEdit(true);
                        wrti[15].setFieldOrder(0);
                        wrti[15].setMand(false);
                        //员工子组
                        String strSubgroup = Util.null2String(formalUser.getStrSubgroup());
                        wrti[16] = new WorkflowRequestTableField();
                        wrti[16].setFieldName("ygzz");
                        wrti[16].setFieldValue(strSubgroup);
                        wrti[16].setView(true);
                        wrti[16].setEdit(true);
                        wrti[16].setFieldOrder(0);
                        wrti[16].setMand(false);
                        //人事范围
                        String strPersonnelScope = Util.null2String(formalUser.getStrPersonnelScope());
                        wrti[17] = new WorkflowRequestTableField();
                        wrti[17].setFieldName("rsfw");
                        wrti[17].setFieldValue(strPersonnelScope);
                        wrti[17].setView(true);
                        wrti[17].setEdit(true);
                        wrti[17].setFieldOrder(0);
                        wrti[17].setMand(false);
                        //人事子范围
                        String strPersonnelSubScope = Util.null2String(formalUser.getStrPersonnelSubScope());
                        wrti[18] = new WorkflowRequestTableField();
                        wrti[18].setFieldName("rszfw");
                        wrti[18].setFieldValue(strPersonnelSubScope);
                        wrti[18].setView(true);
                        wrti[18].setEdit(true);
                        wrti[18].setFieldOrder(0);
                        wrti[18].setMand(false);
                        //工资核算范围
                        String strSalaryScope = Util.null2String(formalUser.getStrSalaryScope());
                        wrti[19] = new WorkflowRequestTableField();
                        wrti[19].setFieldName("gzfw");
                        wrti[19].setFieldValue(strSalaryScope);
                        wrti[19].setView(true);
                        wrti[19].setEdit(true);
                        wrti[19].setFieldOrder(0);
                        wrti[19].setMand(false);
                        //辅导员
                        String strInstructor = Util.null2String(formalUser.getStrInstructor());
                        wrti[20] = new WorkflowRequestTableField();
                        wrti[20].setFieldName("fdy");
                        wrti[20].setFieldValue(strInstructor);
                        wrti[20].setView(true);
                        wrti[20].setEdit(true);
                        wrti[20].setFieldOrder(0);
                        wrti[20].setMand(false);

                        //薪资类型
                        String strSalayScope = Util.null2String(formalUser.getStrSalayScope());
                        wrti[21] = new WorkflowRequestTableField();
                        wrti[21].setFieldName("xzlx");
                        wrti[21].setFieldValue(strSalayScope);
                        wrti[21].setView(true);
                        wrti[21].setEdit(true);
                        wrti[21].setFieldOrder(0);
                        wrti[21].setMand(false);
                        //薪资区域
                        String strSalaryArea = Util.null2String(formalUser.getStrSalaryArea());
                        wrti[22] = new WorkflowRequestTableField();
                        wrti[22].setFieldName("xzqy");
                        wrti[22].setFieldValue(strSalaryArea);
                        wrti[22].setView(true);
                        wrti[22].setEdit(true);
                        wrti[22].setFieldOrder(0);
                        wrti[22].setMand(false);
                        //薪资岗级别
                        String strSalaryPositionLevel = Util.null2String(formalUser.getStrSalaryPositionLevel());
                        wrti[23] = new WorkflowRequestTableField();
                        wrti[23].setFieldName("xzgjb");
                        wrti[23].setFieldValue(strSalaryPositionLevel);
                        wrti[23].setView(true);
                        wrti[23].setEdit(true);
                        wrti[23].setFieldOrder(0);
                        wrti[23].setMand(false);
                        //薪资岗档次
                        String strSalaryPostGrade = Util.null2String(formalUser.getStrSalaryPostGrade());
                        wrti[24] = new WorkflowRequestTableField();
                        wrti[24].setFieldName("xzgdc");
                        wrti[24].setFieldValue(strSalaryPostGrade);
                        wrti[24].setView(true);
                        wrti[24].setEdit(true);
                        wrti[24].setFieldOrder(0);
                        wrti[24].setMand(false);
                        //基本工资
                        String strBasePay = Util.null2String(formalUser.getStrInstructor());
                        wrti[25] = new WorkflowRequestTableField();
                        wrti[25].setFieldName("jbgz");
                        wrti[25].setFieldValue(strBasePay);
                        wrti[25].setView(true);
                        wrti[25].setEdit(true);
                        wrti[25].setFieldOrder(0);
                        wrti[25].setMand(false);
                        //绩效工资
                        String strMeritPay = Util.null2String(formalUser.getStrMeritPay());
                        wrti[26] = new WorkflowRequestTableField();
                        wrti[26].setFieldName("jxgz");
                        wrti[26].setFieldValue(strMeritPay);
                        wrti[26].setView(true);
                        wrti[26].setEdit(true);
                        wrti[26].setFieldOrder(0);
                        wrti[26].setMand(false);
                        //年终奖金基数
                        String strYearEndBase = Util.null2String(formalUser.getStrYearEndBase());
                        wrti[27] = new WorkflowRequestTableField();
                        wrti[27].setFieldName("nzjjjs");
                        wrti[27].setFieldValue(strYearEndBase);
                        wrti[27].setView(true);
                        wrti[27].setEdit(true);
                        wrti[27].setFieldOrder(0);
                        wrti[27].setMand(false);


                        //转正人姓名
                        String strUserid = Util.null2String(formalUser.getStrUserid());
                        wrti[28] = new WorkflowRequestTableField();
                        wrti[28].setFieldName("zzrxm");
                        wrti[28].setFieldValue(strUserid);
                        wrti[28].setView(true);
                        wrti[28].setEdit(true);
                        wrti[28].setFieldOrder(0);
                        wrti[28].setMand(false);
                        //填报人
                        String strTbr = Util.null2String(formalUser.getStrDepartmentCharge());
                        wrti[29] = new WorkflowRequestTableField();
                        wrti[29].setFieldName("tbr");
                        wrti[29].setFieldValue(strTbr);
                        wrti[29].setView(true);
                        wrti[29].setEdit(true);
                        wrti[29].setFieldOrder(0);
                        wrti[29].setMand(false);
                        //填报时间
                        String strTbsj = Util.null2String(strCurrDate);
                        wrti[30] = new WorkflowRequestTableField();
                        wrti[30].setFieldName("tbsj");
                        wrti[30].setFieldValue(strTbsj);
                        wrti[30].setView(true);
                        wrti[30].setEdit(true);
                        wrti[30].setFieldOrder(0);
                        wrti[30].setMand(false);
                        //获取年假调余额
                        String strNjye = "";
                        //调休余额
                        String strTxye = "";
                        String strReturn = getDatas(formalUser.getStrUserid(), strTbsj);
                        if (!"".equals(strReturn)) {
                            strNjye = strReturn.split(",")[0];
                            strTxye = strReturn.split(",")[1];

                        }
                        //年假余额

                        wrti[31] = new WorkflowRequestTableField();
                        wrti[31].setFieldName("njye");
                        wrti[31].setFieldValue(strNjye);
                        wrti[31].setView(true);
                        wrti[31].setEdit(true);
                        wrti[31].setFieldOrder(0);
                        wrti[31].setMand(false);

                        wrti[32] = new WorkflowRequestTableField();
                        wrti[32].setFieldName("dxye");
                        wrti[32].setFieldValue(strTxye);
                        wrti[32].setView(true);
                        wrti[32].setEdit(true);
                        wrti[32].setFieldOrder(0);
                        wrti[32].setMand(false);
                        //是否调休为 否
                        wrti[33] = new WorkflowRequestTableField();
                        wrti[33].setFieldName("sfdx");
                        wrti[33].setFieldValue("1");
                        wrti[33].setView(true);
                        wrti[33].setEdit(true);
                        wrti[33].setFieldOrder(0);
                        wrti[33].setMand(false);
                        //下一节点审批人
                        wrti[34] = new WorkflowRequestTableField();
                        wrti[34].setFieldName("xyjdspr");
                        wrti[34].setFieldValue(formalUser.getStrDepartmentCharge());
                        wrti[34].setView(true);
                        wrti[34].setEdit(true);
                        wrti[34].setFieldOrder(0);
                        wrti[34].setMand(false);

                        wrti[35] = new WorkflowRequestTableField();
                        wrti[35].setFieldName("czlx");
                        wrti[35].setFieldValue("0");
                        wrti[35].setView(true);
                        wrti[35].setEdit(true);
                        wrti[35].setFieldOrder(0);
                        wrti[35].setMand(false);

                        wrti[36] = new WorkflowRequestTableField();
                        wrti[36].setFieldName("czyy");
                        wrti[36].setFieldValue("0");
                        wrti[36].setView(true);
                        wrti[36].setEdit(true);
                        wrti[36].setFieldOrder(0);
                        wrti[36].setMand(false);

                        //装填主表信息
                        WorkflowRequestTableRecord[] wrtri = new WorkflowRequestTableRecord[1];
                        wrtri[0] = new WorkflowRequestTableRecord();
                        wrtri[0].setWorkflowRequestTableFields(wrti);
                        WorkflowMainTableInfo wmi = new WorkflowMainTableInfo();
                        wmi.setRequestRecords(wrtri);

                        //基本信息
                        WorkflowBaseInfo wbi = new WorkflowBaseInfo();
                        //转正流程workflowid
                        String strWorkflowid = "";
                        if ("0".equals(formalUser.getStrLcid())) {
                            strWorkflowid = Util.null2String(bb.getPropValue("humanMess", "formalWorkflowid"));
                        } else {
                            strWorkflowid = Util.null2String(bb.getPropValue("humanMess", "fgsformalWorkflowid"));
                        }
                        wbi.setWorkflowId(strWorkflowid);
                        WorkflowRequestInfo wri = new WorkflowRequestInfo();
                        //创建人为部门负责人
                        wri.setCreatorId(strCreater);
                        //紧急程度
                        wri.setRequestLevel("0");
                        //请求的名称
                        wri.setRequestName("人员转正流程");
                        //停留在待办
                        // wri.setIsnextflow("0");

                        //主表数据
                        wri.setWorkflowMainTableInfo(wmi);
                        wri.setWorkflowBaseInfo(wbi);
                        WorkflowServiceImpl wfsi = new WorkflowServiceImpl();

                        String requestid = wfsi.doCreateWorkflowRequest(wri, Integer.parseInt(strCreater));
                        log.info("批量生成流程返回值==" + requestid);

                    }
                }

            }
        }catch (Exception ex){
            log.info("程序异常："+ex.toString());
        }

    }

    /**
     * 获取年假和调休余额
     *
     * @param strUserid
     * @param strTbsj
     * @return
     */
    public String getDatas(String strUserid, String strTbsj) {


        //获取年假余额
        String strNjye = KQBalanceOfLeaveBiz.getRestAmount(strUserid, "2", strTbsj);
        log.info("年假余额：" + strNjye);
        //获取调休假余额
        String strTxjye = KQBalanceOfLeaveBiz.getRestAmount(strUserid, "5", strTbsj);
        log.info("调休假余额:" + strTxjye);
        //拼接返回值
        String strReturn = strNjye + "," + strTxjye;


        return strReturn;


    }

}
